# Defining functions to output LaTeX tables with regression results

make_table_4aidvars <- function(trust_index_lm,
                                democ_high_qual_lm,
                                refuse_pay_tax_lm,
                                gov_right_tax_lm,
                                easy_avoid_tax_lm,
                                keep,
                                covariate.labels = c("China Completed","China Planned","USA Completed","USA Planned"),
                                pvale_title_1 = "China",
                                pvale_title_2 = "USA",
                                file){
  
  # Make stagazer table when where are four aid covariates. 
  
  stargazer(trust_index_lm,
            democ_high_qual_lm,
            refuse_pay_tax_lm,
            gov_right_tax_lm,
            easy_avoid_tax_lm,
            dep.var.labels.include = T,
            dep.var.labels = c("Trust in Gov ","Believes Democ","Has Refused","Believes Govt","Believes Easy"),
            column.labels   = c("[Index]",     "Is High Qual", "Pay Tax",     "Right to Tax",  "Avoid Tax"),
            keep=keep,
            covariate.labels = covariate.labels,
            dep.var.caption = "",
            omit.stat = c("f","ser"), 
            align=TRUE,
            no.space=TRUE,
            float=FALSE,
            column.sep.width = "8pt",
            report="vcs*",
            digits = 2,
            add.lines = list(
              c(paste0(pvale_title_1, ": Completed vs. planned $p$-value"), 
                tryCatch(round(linearHypothesis(trust_index_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(democ_high_qual_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(refuse_pay_tax_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(gov_right_tax_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(easy_avoid_tax_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")) ),
              c(paste0(pvale_title_2, ": Completed vs. planned $p$-value"), 
                tryCatch(round(linearHypothesis(trust_index_lm, paste(keep[3],"= ",keep[4]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(democ_high_qual_lm, paste(keep[3],"= ",keep[4]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(refuse_pay_tax_lm, paste(keep[3],"= ",keep[4]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(gov_right_tax_lm, paste(keep[3],"= ",keep[4]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(easy_avoid_tax_lm, paste(keep[3],"= ",keep[4]))[2,4],3), error = function(e) print("NA")) ),
              c("Country Fixed Effects", "Y", "Y", "Y", "Y", "Y"),
              c("Afro. Round Fixed Effects", "Y", "Y", "Y", "Y", "Y")),
            out=file)
  
} 

make_table_2aidvars <- function(trust_index_lm,
                                democ_high_qual_lm,
                                refuse_pay_tax_lm,
                                gov_right_tax_lm,
                                easy_avoid_tax_lm,
                                keep = keep,
                                covariate.labels = covariate.labels,
                                file = ""){
  
  # Make stagazer table when where are two aid covariates. 
  
  stargazer(trust_index_lm,
            democ_high_qual_lm,
            refuse_pay_tax_lm,
            gov_right_tax_lm,
            easy_avoid_tax_lm,
            dep.var.labels.include = T,
            dep.var.labels = c("Trust in Gov ","Believes Democ","Has Refused","Believes Govt","Believes Easy"),
            column.labels   = c("[Index]",     "Is High Qual", "Pay Tax",     "Right to Tax",  "Avoid Tax"),
            keep=keep,
            covariate.labels = covariate.labels,
            dep.var.caption = "",
            omit.stat = c("f","ser"), 
            align=TRUE,
            no.space=TRUE,
            float=FALSE,
            column.sep.width = "8pt",
            report="vcs*",
            digits = 2,
            add.lines = list(
              c("Completed vs. planned $p$-value", 
                tryCatch(round(linearHypothesis(trust_index_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(democ_high_qual_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(refuse_pay_tax_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(gov_right_tax_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")),
                tryCatch(round(linearHypothesis(easy_avoid_tax_lm, paste(keep[1],"= ",keep[2]))[2,4],3), error = function(e) print("NA")) ),
              c("Country Fixed Effects", "Y", "Y", "Y", "Y", "Y"),
              c("Afro. Round Fixed Effects", "Y", "Y", "Y", "Y", "Y")),
            out=file)
}